<template>
  <div class="app-container">
    <div class="filter-container">
        <el-row :gutter="20">
            <el-col :span="4">
                <el-input v-model="listQuery.title" placeholder="标题名称" class="filter-item" @keyup.enter.native="handleFilter"/>
            </el-col>

            <el-col :span="3">
                <el-select v-model="listQuery.cid" placeholder="分类" clearable class="filter-item" @change="handleFilter">
                    <el-option v-for="item in cateList" :key="item.key" :label="item.name" :value="item.key" />
                </el-select>
            </el-col>

            <el-col :span="3">
                <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
            </el-col>
        </el-row>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="300px" label="标题">
        <template slot-scope="{row}">
          <router-link :to="'/article/edit/'+row.id" class="link-type">
            <span>{{ row.title }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column min-width="150px" label="所属分类">
        <template slot-scope="{row}">
          <router-link :to="'/article/edit/'+row.id" class="link-type">
            <span>{{ row.catename }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column min-width="80px" label="作者">
        <template slot-scope="{row}">
          <router-link :to="'/article/edit/'+row.id" class="link-type">
            <span>{{ row.auther }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column min-width="80px" label="排序">
        <template slot-scope="{row}">
          <router-link :to="'/article/edit/'+row.id" class="link-type">
            <span>{{ row.sort }}</span>
          </router-link>
        </template>
      </el-table-column>

      <el-table-column min-width="80px" label="是否显示">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.hidden"
            :active-value="0"
            :inactive-value="1"
            @change="switchChange($event, row)"
            active-color="#13ce66"
            inactive-color="#ff4949"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column width="200px" align="center" label="创建时间">
        <template slot-scope="scope">
          <span>{{ scope.row.addTime | parseTimeFormat('YYYY-MM-DD HH:mm:ss') }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <router-link :to="'/article/edit/'+scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit" style="margin-right:10px">编辑</el-button>
          </router-link>
          <el-button
            @click="deletearticle(scope.row.id)"
            size="small"
            type="danger"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :current-page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="changePage"
    />
  </div>
</template>

<script>
import { articleList, deleteArticle, changeState } from "@/api/article";
import { articleCateList } from "@/api/article-cate";
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination

export default {
  name: "ArticleList",
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        cid: null,
        title: null,
        pageNo: 0,
        pageSize: 20,
        pageType: "P"
      },
      cateList: [{ key: null, name: "选择分类" }]
    };
  },
  created() {
    this.getCateList();
    this.getList();
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 0;
      this.getList();
    },
    // 分页
    changePage(value) {
      this.listQuery.pageNo = value.page - 1;
      this.listQuery.pageSize = value.limit;
      this.getList();
    },
    // 获取文章
    getList() {
      console.log(this.listQuery);
      this.listLoading = true;
      articleList(this.listQuery).then(response => {
        this.list = response.data;
        this.total = response.count;
        this.listLoading = false;

        // 处理分类名称显示
        for (let i in this.list) {
          for (let p in this.cateList)
          {
            if (this.list[i].cid == this.cateList[p].key)
            {
                this.list[i]['catename'] = this.cateList[p].name;
            }
          }
        }
      });
    },
    // 获取文章分类
    getCateList() {
      articleCateList({ pageType: "A" }).then(response => {
        let data = response.data;
        for (let i in data) {
          if (data[i].hidden == 0)
          {
            this.cateList.push({ key: data[i].id, name: data[i].title });
          }
        }
      });
    },
    // 删除
    deletearticle(id) {
      let that = this;
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteArticle({
          id: id
        }).then(res => {
          if (res.success == true) {
            this.$notify({
              title: "删除成功",
              message: "文章分类删除成功",
              type: "success",
              duration: 2000
            });
            that.getList();
          }
        });
      }).catch(() => {
        return false;      
      });
    },
    switchChange(e, data) {
      changeState({ id: data.id, hidden: data.hidden }).then(response => {});
    }
  }
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
